<template>
	<view class="echart-box">
		<view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" :id="option.id" class="echarts"></view>
	</view>
</template>

<script>
	import echarts from 'echarts'
	import {parseTime} from '@/utils/index.js'
	export default {
		props: {
			chartId: {
				type: String,
				default: 'echarts-line'
			},
			xAxis: {
				type: Array,
				default: function () {
					return []
				}
			},
			series: {
				type: Array,
				default: function () {
					return []
				}
			},
			series1: {
				type: Array,
				default: function () {
					return []
				}
			}
		},
		data(options) {
			let chartId = new Date().getTime() + ''
			for(var i=0;i<6;i++) {
			    chartId += Math.floor(Math.random()*10)
			}
			return {
				option: {
					id: chartId,
					grid: {
						top: '30px',
						right: '0',
						bottom: '30px',
						left: '40px',
					},
					tooltip: {
						trigger: 'axis',
						position: function (pt) {
							return [pt[0]+'%', '10%'];
						},
						// formatter: '{b0}<br/> {a}:{c0}%',<br/>{a1}:{c1}%
						formatter: '{b0}<br/> {a0}:{c0}%'
					},
					xAxis: {
						type: 'category',
						boundaryGap: false,
						data: options.xAxis
					},
					// yAxis: {
					// 	type: 'value',
					// 	boundaryGap: [0, '100%']
					// },
					yAxis: [{
						type: 'value',
						// name: '温度',
						// axisLine: {
						// 	lineStyle: {
						// 		color: "#ccc"
						// 	}
						// },
						axisLabel: {
							formatter: '{value}'+'%'
						}
					}],
					dataZoom: [{
						type: 'inside',
						start: 0,
						end: 100,
						preventDefaultMouseMove: false
					}],
					series: [
						// {
						// 	name: '沪深300',
						// 	type: 'line',
						// 	smooth: true,
						// 	symbol: 'none',
						// 	sampling: 'average',
						// 	itemStyle: {
						// 		color: 'rgb(39, 118, 182)',
						// 	},
						// 	data: options.series1
						// },
						//基准收益
						{
							name: '策略收益',
							type: 'line',
							smooth: true,
							symbol: 'none',
							sampling: 'average',
							howAllSymbol: true,
							itemStyle: {
								color: 'rgb(255, 0, 0)',
							},
							areaStyle: {
								color: 'rgb(247, 202, 201)'
							},
							data: options.series
						}
						// {
						// 	name: '模拟数据2',
						// 	type: 'line',
						// 	smooth: true,
						// 	symbol: 'none',
						// 	sampling: 'average',
						// 	itemStyle: {
						// 		color: 'rgb(143, 78, 79)'
						// 	},
						// 	data: options.series['relative']
						// }
					]
				}
			}
		},
		mounted() {},
		methods: {
			changeOption() {},
			onViewClick(options) {}
		}
	}
</script>

<script module="echarts" lang="renderjs">
	import echarts from 'echarts'
	let myChart
	export default {
		// #ifndef APP-PLUS
		mounted() {
			this.initEcharts()
		},
		// #endif
		
		// #ifdef APP-PLUS
		beforeUpdate() {
			this.initEcharts()
		},
		// #endif
		methods: {
			//	初始化图表
			initEcharts() {
				myChart = echarts.init(document.getElementById(this.option.id))
				// 观测更新的数据在 view 层可以直接访问到
				myChart.setOption(this.option)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				alert(JSON.stringify(newValue))
				// 监听 service 层数据变更
				myChart.setOption(newValue)
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			}
		}
	}
</script>

<style>
	.echart-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.echarts {
		width: 100%;
		height: 400rpx;
	}
</style>
